/*
 * Copyright (C) 2020  Prasoon Joshi
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package persistence

import org.apache.tinkerpop.gremlin.neo4j.structure.Neo4jGraph
import gremlin.scala._
import org.scalatest._
import utils.FileUtils

class DataRepositorySpec extends FlatSpec with Matchers {

  "Gremlin-scala" should "create some vertices with properties" in {
    val dbPath = "target/tinkerPopTest"
    FileUtils.removeAll(dbPath)
    val graph = Neo4jGraph.open(dbPath).asScala()
    val Name = Key[String]("name")

    (1 to 5) foreach { i =>
      graph + ("some_label", Name -> s"vertex $i")
    }

    val names  = graph.V().value(Name).toList()
    names.size shouldBe 5
    names foreach (_.startsWith("vertex") shouldBe true)

    graph.close()
  }

  "gremlin-scala" should "create the sample ead3 graph" in {
    val dbPath = "target/ead3Test"
    FileUtils.removeAll(dbPath)
    val graph = Neo4jGraph.open(dbPath).asScala()

    val id = Key[String]("id")
    val audience = Key[String]("audience")
    graph +
      ("Ead",
        id -> "f8b405d6-fc4f-4ae8-adeb-7ddb07b758bd",
        audience -> "external"
      )

    val countryEncoding = Key[String]("countryEncoding")
    val dateEncoding = Key[String]("dateEncoding")
    val langEncoding = Key[String]("langEncoding")
    val repositoryEncoding = Key[String]("repositoryEncoding")
    val scriptEncoding = Key[String]("scriptEncoding")
    graph +
      ("control",
        countryEncoding -> "iso3166-1",
        dateEncoding -> "iso8601",
        langEncoding -> "iso639-2b",
        repositoryEncoding -> "iso15511",
        scriptEncoding -> "iso15924"
      )

    val instanceUrl = Key[String]("instanaceUrl")
    val textValue = Key[String]("textValue")
    graph +
      ("recordId",
        instanceUrl -> "http://library.marist.edu/archives/LTP/LTP.xml",
        textValue -> "1.1"
      )
  }
}
